/**
 * MemberProfile.java
 * 
 * Copyright@2015 OVT Inc. All rights reserved. 
 * 
 * Dec 29, 2015
 */
package com.ovt.doorbell.payment.dao.vo;

import java.util.List;

import com.ovt.doorbell.payment.common.utils.CollectionUtils;
import com.ovt.doorbell.payment.dao.constant.ResourceType;

/**
 * MemberProfile:
 * 
 * (1) resources field will always has 2 elements: space, flow;
 * if user didn't buy any resource, they are default free ones.
 * 
 * @Author zhi.liu
 * @Version 1.0
 * @See
 * @Since [OVT Cloud Platform]/[API] 1.0
 */
public class MemberProfile
{

    private String userId;

    private String deviceId;

    private List<MemberResource> resources;

    public MemberResource getResource(ResourceType type)
    {
        if (CollectionUtils.isNotEmpty(resources))
        {
            for (MemberResource memberResource : resources)
            {
                if (memberResource.getResourceType() == type)
                {
                    return memberResource;
                }
            }
        }

        return null;
    }

    public int getResourceSize(ResourceType type)
    {
        int size = 0;
        MemberResource memberResource = this.getResource(type);

        // MemberProfileService should make sure MemberProfile's resource is not
        // empty, at least has default free resource.
        if (memberResource != null)
        {
            size = memberResource.getResourceSize();
        }

        return size;
    }
    
    public void removeResource(MemberResource oldResource)
    {
        if (oldResource != null && CollectionUtils.isNotEmpty(resources))
        {
            this.resources.remove(oldResource);
        }
    }
    
    public void replaceResource(ResourceType type, MemberResource newResource)
    {
        if (CollectionUtils.isNotEmpty(resources))
        {
            MemberResource oldResource = null;
            for (MemberResource memberResource : resources)
            {
                if (memberResource.getResourceType() == type)
                {
                    oldResource = memberResource;
                    break;
                }
            }
            
            if (oldResource != null)
            {
                this.resources.remove(oldResource);
            }
            this.resources.add(newResource);
        }
    }

    public List<MemberResource> getResources()
    {
        return resources;
    }

    public void setResources(List<MemberResource> resources)
    {
        this.resources = resources;
    }

    public String getUserId()
    {
        return userId;
    }

    public void setUserId(String userId)
    {
        this.userId = userId;
    }

    public String getDeviceId()
    {
        return deviceId;
    }

    public void setDeviceId(String deviceId)
    {
        this.deviceId = deviceId;
    }

}
